<template>
  <div>
    <el-dialog
      :title="isEdit ? '编辑部门' : '添加子部门'"
      :show-close="false"
      :visible="dialogVisiblefn"
      @update:visible="(val) => $emit('upcancel')"
      width="50%"
      :before-close="handleClose"
      :close-on-click-modal='false'
      :close-on-press-escape='false'
    >
      <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="部门名称" prop="name">
          <el-input
            v-model="form.name"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input
            v-model="form.code"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select
            v-model="form.manager"
            style="width: 80%"
            placeholder="请选择"
          >
            <el-option
              :value="item.username"
              v-for="(item, index) in employess"
              :key="index"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="form.introduce"
            style="width: 80%"
            placeholder="1-300个字符"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="certain">确 定</el-button>
      </span>
    
    </el-dialog>
   
  </div>
</template>

<script>
export default {
  data() {
    const validatorcode = (rule, value, callback) => {
     if(this.isEdit==false){
       const codelistt = this.originlist.map((item) => {
        return item.code;
      });
      codelistt.includes(value)
        ? callback(new Error(`值以及存在 ${value}`))
        : callback();
     }
    };
const validname=(rule,value,callback)=>{
 const namelist=this.originlist.filter(item=>{
  return item.pid==this.clickpid
 }).map(item=>item.name)
    namelist.includes(value) ? callback(new Error(`已经有值了`)) : callback()


    };
    return {
      // dialogVisible: false
      form: {
        name: "", // 部门名称
        code: "", // 部门编码
        manager: "", // 部门管理者
        introduce: "", // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "部门名称要求1-50个字符",
            trigger: "blur",
          },
          { validator: validname, trigger: "blur" },
        ],
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "部门编码要求1-50个字符",
            trigger: "blur",
          },

          { validator: validatorcode, trigger: "blur" },
        ],
        manager: [
          { required: true, message: "部门负责人不能为空", trigger: "change" },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          {
            min: 1,
            max: 300,
            message: "部门介绍要求1-300个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  props: {
    dialogVisiblefn: {
      type: Boolean,
    },
    employess: {
      type: Array,
      default: () => [],
    },
    //父组件穿过来的校验数据
    originlist: {
      type: Array,
      default: () => [],
    },
    //记录是添加还是编辑的状态
    isEdit: {
      type: Boolean,
      // default:false
    },
    //点击添加是传过来的ID
    clickpid:{
      type:String
    }
  },
  // watch:{
  // dialogVisiblefn(){
  //   this.dialogVisible=this.dialogVisiblefn
  // },
  // },

  created() {
      // console.log(this.isEdit);
    // console.log(this.clickpid,'llll');
  },
  mounted() {
    // console.log(this.clickpid);
  },
  methods: {
 
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //点击取消按钮
    cancel() {
      this.$emit("upcancel", false);
      this.form = {
        name: "", // 部门名称
        code: "", // 部门编码
        manager: "", // 部门管理者
        introduce: "", // 部门介绍
      };
      //对表单内容进行重置，并且清除校验内容
      this.$refs.deptForm.resetFields();
    },
    //点击确定按钮
    certain() {
      //    this.$refs.deptForm.validate((valid) => {
      //    }
      this.$refs.deptForm.validate((valid) => {
        if (valid) {
          this.$emit("addupcertain", this.form);
        }
      });
    },
  },
};
</script>

<style scoped lang="less">
</style>
